package com.deer.wms.instock.web;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.ASN.manage.service.AsnDetailService;
import com.deer.wms.ASN.manage.service.AsnMasterService;
import com.deer.wms.instock.model.transfer.TransferBill;
import com.deer.wms.instock.service.TransferBillService;
import com.deer.wms.intercept.annotation.NoRepeatSubmit;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.system.manage.service.ValidatorService;
import com.deer.wms.ware.task.model.SO.SoData;
import com.deer.wms.ware.task.model.SO.SoDetail;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.service.SoMasterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zy
 * @since 2020/06/27
 */
@Api(description = "SO信息api接口")
@RestController
@RequestMapping("/so/newMasters")
@Transactional
public class SoMaterNewController {
    @Autowired
    private SoMasterService soMasterService;
    @Autowired
    private ValidatorService validatorService;
    @Autowired
    private AsnMasterService asnMasterService;
    @Autowired
    private AsnDetailService asnDetailService;
    @Autowired
    private TransferBillService transferBillService;

    @ApiOperation(value = "新增SO信息", notes = "新增SO信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody SoData soData, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        String soTableName = "so_master";
        Integer soNextId = validatorService.getNextId(soTableName);
        String soBillNo = "SO" + "-" + soNextId;
        soData.getSoMaster().setBillNo(soBillNo);
        soMasterService.createSoMaster(soData, currentUser);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "生产退料", notes = "生产退料入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/PDreturn/{billNo}")
    @NoRepeatSubmit
    public Result PDreturn(@RequestBody List<SoDetail> soDetails, @PathVariable(value = "billNo") String billNo,@ApiIgnore @User CurrentUser currentUser) {
        List<SoDetail> details = soDetails.stream().filter(item -> item.getNewReturnQty() > 0).collect(Collectors.toList());
        if (ObjectUtil.isEmpty(details)) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "退料数量不能为空");
        }

        transferBillService.PDreturn(details, billNo,currentUser);
        return ResultGenerator.genSuccessResult();
    }
}
